package ar.com.mooral.activities;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import ar.com.mooral.crohn.application.CrohnContext;
import ar.com.mooral.data.Result;
import ar.com.mooral.pojo.MissedMedicationPojo;

public class SeleccionRangos extends CrohnActivity {

	static final int DESDE_DIALOG_ID = 0;
	static final int HASTA_DIALOG_ID = 1;
	
	protected void onStart() {
		super.onStart();

		Button btnDesde = (Button) findViewById(R.id.btnSeleccionRangosDesde);
		if (btnDesde.getText().toString().equals("")) {
			btnDesde.setText(CrohnContext.getHumanDate(CrohnContext.getDate()));
		} else {
	        Button btnHasta = (Button) findViewById(R.id.btnSeleccionRangosHasta);
			btnHasta.setText(CrohnContext.getHumanDate(CrohnContext.getDate()));
		}

	}	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.seleccion_rangos);


        // SELECCION FECHA DESDE
        Button btnDesde = (Button) findViewById(R.id.btnSeleccionRangosDesde);
        btnDesde.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	showDialog(DESDE_DIALOG_ID);
		    }
		});

        
        // SELECCION FECHA HASTA
        Button btnHasta = (Button) findViewById(R.id.btnSeleccionRangosHasta);
        btnHasta.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	showDialog(HASTA_DIALOG_ID);
		    }
		});
        
        Button btnSeleccionRangosCancelar = (Button) findViewById(R.id.btnSeleccionRangosCancelar);
        btnSeleccionRangosCancelar.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	finish();
		    }
		});
		    	

        // VER
        Button btnSeleccionRangosVer = (Button) findViewById(R.id.btnSeleccionRangosVer);
        btnSeleccionRangosVer.setOnClickListener (new View.OnClickListener() {
		    public void onClick(View view) {
		    	
		    	String desde = ((Button) findViewById(R.id.btnSeleccionRangosDesde)).getText().toString();
		    	String hasta = ((Button) findViewById(R.id.btnSeleccionRangosHasta)).getText().toString();
		    	
		    	if (desde.equals("") || hasta.equals("")) {
		    		Result r = new Result(Result.CRITICAL, "Debe seleccionar el rango completo de fechas para continuar.");
		    		showMessage(r);
		    	} else {
		    		CrohnContext.setRecordsMinRange(desde + " 00:00:00");
		    		CrohnContext.setRecordsMaxRange(hasta + " 23:59:59");
		    		
		    		finish();
					//Intent intent = new Intent();
					//intent.setComponent(new ComponentName(Registros.class.getPackage().getName(), Registros.class.getName()));
					//startActivity(intent);
	         		//TabGroupActivity parentActivity = (TabGroupActivity)getParent().getParent();
					//TabGroupActivity parentActivity = (TabGroupActivity)getParent();
	         		//parentActivity.startChildActivity("Registros", intent);
					
		    		
		    	}
		    }
		});

    }
    
    
	// Creating dialog
	@Override
	protected Dialog onCreateDialog(int id) {
		Calendar c = Calendar.getInstance();
		int cyear = c.get(Calendar.YEAR);
		int cmonth = c.get(Calendar.MONTH);
		int cday = c.get(Calendar.DAY_OF_MONTH);
		switch (id) {
			case DESDE_DIALOG_ID:
				return new DatePickerDialog(this,  mDesdeDateSetListener,  cyear, cmonth, cday);
			case HASTA_DIALOG_ID:
				return new DatePickerDialog(this,  mHastaDateSetListener,  cyear, cmonth, cday);
		}
		return null;
	}
	
	private DatePickerDialog.OnDateSetListener mDesdeDateSetListener = new DatePickerDialog.OnDateSetListener() {
		// onDateSet method
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

			CrohnContext.setDateDay(dayOfMonth);
			CrohnContext.setDateMonth(monthOfYear);
			CrohnContext.setDateYear(year);
			CrohnContext.setDateLoaded(true);
			
			((Button) findViewById(R.id.btnSeleccionRangosDesde)).setText(CrohnContext.getDate());			
		}
	};
    
	private DatePickerDialog.OnDateSetListener mHastaDateSetListener = new DatePickerDialog.OnDateSetListener() {
		// onDateSet method
		public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

			CrohnContext.setDateDay(dayOfMonth);
			CrohnContext.setDateMonth(monthOfYear);
			CrohnContext.setDateYear(year);
			CrohnContext.setDateLoaded(true);
			
			((Button) findViewById(R.id.btnSeleccionRangosHasta)).setText(CrohnContext.getDate());			
		}
	};

}
